package com.gcxy.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @Author: qinlun
 * @Date: 2021/8/18 16:32
 * @Content: json校验工具类
 */
@Slf4j
public class JSONUtil {

    public static <T> T stringMapperT(String string, Class<T> valueType) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return objectMapper.readValue(string, valueType);
        } catch (Exception e) {
            log.error("json 转换异常 ：{}， {} ", e, string);
            throw new RuntimeException("json mapper exception");
        }
    }

    public static <T> T streamMapperT(InputStream stream, Class<T> valueType) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return objectMapper.readValue(stream, valueType);
        } catch (Exception e) {
            throw new RuntimeException("json mapper exception");
        }
    }

    public static <T> List<T> stringMapperTList(String string, Class<T> valueType) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, valueType);
            return objectMapper.readValue(string, javaType);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            log.error("json 转换异常 ：{}， {} ", e, string);
            throw new RuntimeException("json mapper exception");
        }
    }

    public static <T, BEAN> BEAN stringMapperTObject(String string, Class<BEAN> beanType, Class<T> valueType) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(beanType, valueType);
            return objectMapper.readValue(string, javaType);
        } catch (Exception e) {
            log.error("json 转换异常 ：{}， {} ", e, string);
            throw new RuntimeException("json mapper exception");
        }
    }

    public static <T> Map<String, T> stringMapperTMap(String string, Class<T> valueType) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(Map.class, String.class, valueType);
            return objectMapper.readValue(string, javaType);
        } catch (Exception e) {
            log.error("json 转换异常 ：{}， {} ", e, string);
            throw new RuntimeException("json mapper exception");
        }
    }

    public static String toJson(Object object) {
        try {
            return new ObjectMapper().writeValueAsString(object);
        } catch (Exception e) {
            log.error("json 转换异常 ：{}， {} ", e, object);
            throw new RuntimeException("json mapper exception");
        }
    }

    public static <T> T getJSONForObject(String jsonStr, T t) {
        try {
            return new ObjectMapper().readValue(jsonStr, new TypeReference<T>() {
            });
        } catch (Exception e) {
            log.error("json 转换异常： {},{}", e, jsonStr);
            throw new RuntimeException("jackson convert exception");
        }
    }

    public static String validate(String jsonStr) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        }
        String jsonString = null;
        boolean isJsonObject = true;
        boolean isJsonArray = true;
        try {
            jsonString = JSONObject.parseObject(jsonStr, Feature.OrderedField).toJSONString();
        } catch (Exception e) {
            isJsonObject = false;
        }
        //字符串为Object格式
        if (isJsonObject) {
            return jsonString;
        }
        try {
            jsonString = JSONObject.parseArray(jsonStr).toJSONString();
        } catch (Exception e) {
            isJsonArray = false;
        }
        if (!isJsonArray) {
            return null;
        }
        //字符串为Array格式
        return jsonString;
    }

    /**
     * fastjson转换字符串(json内容保留值为空的key、保留空数组的key以及将时间格式化yyyy-MM-dd HH:mm:ss)
     *
     * @param object
     * @return
     */
    public static String fastjson2String(Object object) {
        return fastjson2String(object, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * fastjson转换字符串(json内容保留值为空的key、保留空数组的key以及将时间格式化yyyy-MM-dd HH:mm:ss)
     *
     * @param object
     * @return
     */
    public static String fastjson2String(Object object, String dateFormat) {
        return JSON.toJSONStringWithDateFormat(object, dateFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteDateUseDateFormat);
    }


}
